/**
 * Created by wlc on 2018/4/9.
 */
/**
 * 获得单选或多选框选择结果
 * @param name
 * @returns {string}
 */
function getCheckboxResult(name) {
    var obj=document.getElementsByName(name);
    var s='';
    for(var i=0; i<obj.length; i++){
        if(obj[i].checked) s+=obj[i].value+','; //如果选中，将value添加到变量s中
    }
    if (s === ''){
        s = "null";
    }
    return s;
}

/**
 * 当select-altInput下拉菜单选择‘其他’，使表单可进行人为填写
 */
$(document).on("click",'.background_form .select-altInput', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.value==="其他" && tar.parentNode.children.length === 2){
        tar.style.width = "100px";
        tar.parentNode.appendChild($('<input type="text" class="form-control" style="width: 150px">')[0])
    }else if (tar.value!=="其他" && tar.parentNode.children.length === 3){
        tar.style.width = "200px";
        tar.parentNode.removeChild(tar.parentNode.lastChild);
    }
});

/**
 * 当input-checkbox表单获得焦点时变为多选,失去焦点时则取消
 */
$(document).on("click",'.background_form', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.className.indexOf("input-checkbox")===-1&&isEmpty($(tar).parents('.show')[0])){
        var ul = document.getElementsByClassName("dropdown-menu show")[0];
        if (!isEmpty(ul)){
            ul.className=ul.className.replace(" show","");
        }
    }
});
$(document).on("focus",'.background_form .input-checkbox', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var ul = tar.parentNode.children[tar.parentNode.children.length-1];
    if (ul.className.indexOf("show") === -1){
        ul.className=ul.className+" show";
    }
});
$(document).on("click",'.background_form .custom-checkbox', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.tagName.toLowerCase()==="input"||tar.tagName.toLowerCase()==="label"){
        var div = tar.parentNode;
        var checkBoxInput = $(tar).parents(".form-group")[0].children[1];
        if(div.children[0].checked){
            checkBoxInput.value = checkBoxInput.value+div.children[1].innerText+",";
        }else {
            checkBoxInput.value = checkBoxInput.value.replace(div.children[1].innerText+",","");
        }
    }
});

/**
 * 当input-unitConversion表单获得焦点时进行单位换算,失去焦点时则取消
 */
$(document).on("click",'.background_form', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.className.indexOf("input-unitConversion")===-1&&isEmpty($(tar).parents('#unitConversion')[0])){
        var unitTool = $('#unitConversion')[0];
        if (!isEmpty(unitTool)){
            unitTool.remove();
        }
    }
});
$(document).on("focus",'.background_form .input-unitConversion', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.parentNode.children.length !== 3){
        var unitTool = $('#unitConversion')[0];
        if (!isEmpty(unitTool)){
            unitTool.remove();
        }
        unitTool = $(unitConversion)[0];
        tar.parentNode.appendChild(unitTool);
    }
});
$(document).on('input propertychange','.background_form .input-unitConversion', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var unitList = $('#unitConversion .tab-content .active')[0];
    var value = tar.value;
    var unit = value.replace(/\d+/g,'').replace(".","");
    switch (unitList.id){
        case 'unitConversion-length':
            var unitItems = unitList.children[0].children;
            if (unit==="m"){
                unitItems[0].children[1].innerText = parseFloat(value); //m
                unitItems[1].children[1].innerText = parseFloat(value)*(100); //cm
                unitItems[2].children[1].innerText = parseFloat(value)*(1000); //mm
                unitItems[3].children[1].innerText = parseFloat(value)*(1000000); //um
                unitItems[4].children[1].innerText = parseFloat(value)*(1000000000); //nm
                unitItems[5].children[1].innerText = parseFloat(value)*(10000000000); //A
            }else if (unit==="cm"){
                unitItems[0].children[1].innerText = parseFloat(value)/(100); //m
                unitItems[1].children[1].innerText = parseFloat(value); //cm
                unitItems[2].children[1].innerText = parseFloat(value)*(10); //mm
                unitItems[3].children[1].innerText = parseFloat(value)*(10000); //um
                unitItems[4].children[1].innerText = parseFloat(value)*(10000000); //nm
                unitItems[5].children[1].innerText = parseFloat(value)*(100000000); //A
            } else if (unit==="mm"){
                unitItems[0].children[1].innerText = parseFloat(value)/(1000); //m
                unitItems[2].children[1].innerText = parseFloat(value)/(10); //cm
                unitItems[2].children[1].innerText = parseFloat(value); //mm
                unitItems[3].children[1].innerText = parseFloat(value)*(1000); //um
                unitItems[4].children[1].innerText = parseFloat(value)*(1000000); //nm
                unitItems[5].children[1].innerText = parseFloat(value)*(10000000); //A
            }else if (unit==="um"){
                unitItems[0].children[1].innerText = parseFloat(value)/(1000000); //m
                unitItems[1].children[1].innerText = parseFloat(value)/(10000); //cm
                unitItems[2].children[1].innerText = parseFloat(value)/(1000); //mm
                unitItems[3].children[1].innerText = parseFloat(value); //um
                unitItems[4].children[1].innerText = parseFloat(value)*(1000); //nm
                unitItems[5].children[1].innerText = parseFloat(value)*(10000); //A
            }else if (unit==="nm"){
                unitItems[0].children[1].innerText = parseFloat(value)/(1000000000); //m
                unitItems[1].children[1].innerText = parseFloat(value)/(10000000); //cm
                unitItems[2].children[1].innerText = parseFloat(value)/(1000000); //mm
                unitItems[3].children[1].innerText = parseFloat(value)/(1000); //um
                unitItems[4].children[1].innerText = parseFloat(value); //nm
                unitItems[5].children[1].innerText = parseFloat(value)*(10); //A
            } else if (unit==="A"){
                unitItems[0].children[1].innerText = parseFloat(value)/(10000000000); //m
                unitItems[1].children[1].innerText = parseFloat(value)/(100000000); //cm
                unitItems[2].children[1].innerText = parseFloat(value)/(10000000); //mm
                unitItems[3].children[1].innerText = parseFloat(value)/(10000); //um
                unitItems[4].children[1].innerText = parseFloat(value)/(10); //nm
                unitItems[5].children[1].innerText = parseFloat(value); //A
            }
            break;
    }
});
$(document).on("click",'#unitConversion .active span', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    $(tar).parents("#unitConversion")[0].parentNode.children[1].value = tar.innerText + tar.parentNode.children[0].innerText;
});